package com.jiangjiang.eshop.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.wms.convert.SaleDeliveryOrderItemConvert;
import com.jiangjiang.eshop.wms.dao.SaleDeliveryOrderItemMapper;
import com.jiangjiang.eshop.wms.domain.SaleDeliveryOrderItemEntity;
import com.jiangjiang.eshop.wms.domain.SaleDeliveryOrderSendOutDetail;
import com.jiangjiang.eshop.wms.dto.SaleDeliveryOrderItemDTO;
import com.jiangjiang.eshop.wms.dto.SaleDeliveryOrderPickingItemDTO;
import com.jiangjiang.eshop.wms.dto.SaleDeliveryOrderSendOutDetailDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/9/17
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class SaleDeliveryOrderItemService extends ServiceImpl<SaleDeliveryOrderItemMapper, SaleDeliveryOrderItemEntity> {
    @Autowired
    private SaleDeliveryOrderItemConvert saleDeliveryOrderItemConvert;

    @Autowired
    private SaleDeliveryOrderPickingItemService saleDeliveryOrderPickingItemService;

    @Autowired
    private SaleDeliveryOrderSendOutDetailService saleDeliveryOrderSendOutDetailService;

    /**
     * 批量保存销售出库明细数据
     *
     * @param itemDTOList 销售出库明细数据
     * @return 处理结果
     */
    public Boolean saveBatch(List<SaleDeliveryOrderItemDTO> itemDTOList) {
        //拣货条目数据收集器
        List<SaleDeliveryOrderPickingItemDTO> pickingItems = Lists.newArrayList();
        //发货明细数据收集器
        List<SaleDeliveryOrderSendOutDetailDTO> sendOutItems = Lists.newArrayList();

        for (SaleDeliveryOrderItemDTO itemDTO : itemDTOList) {
            // 销售出库明细数据保存
            SaleDeliveryOrderItemEntity entity = saleDeliveryOrderItemConvert.convertDTO2Entity(itemDTO);
            this.save(entity);
            // 收集拣货条目数据
            collectPickingItems(entity.getId(), itemDTO, pickingItems);
            // 收集发货条目数据
            collectSendOutItems(entity.getId(), itemDTO, sendOutItems);
        }

        if( CollectionUtils.isNotEmpty(pickingItems)){
            saleDeliveryOrderPickingItemService.saveBatch(pickingItems);
        }

        if( CollectionUtils.isNotEmpty(sendOutItems)){
            saleDeliveryOrderSendOutDetailService.saveBatch(sendOutItems);
        }
        return true;
    }

    /**
     * 拣货条目数据
     *
     * @param saleDeliveryOrderItemId 销售出库条目id
     * @param itemDTO 销售出库明细数据
     * @param pickingItems 拣货条目容器
     */
    private void collectPickingItems(Long saleDeliveryOrderItemId,
                                     SaleDeliveryOrderItemDTO itemDTO,
                                     List<SaleDeliveryOrderPickingItemDTO> pickingItems) {
        // 拣货条目补充数据
        List<SaleDeliveryOrderPickingItemDTO> pickingItemDTOList = itemDTO.getPickingItems()
                .stream()
                .peek(dto -> dto.setSaleDeliveryOrderItemId(saleDeliveryOrderItemId))
                .collect(Collectors.toList());
        pickingItems.addAll(pickingItemDTOList);
    }


    /**
     * 发货条目数据收集
     *
     * @param saleDeliveryOrderItemId 销售出库条目id
     * @param itemDTO 销售出库明细数据
     * @param sendOutItems 发货条目容器
     */
    private void collectSendOutItems(Long saleDeliveryOrderItemId,
                                     SaleDeliveryOrderItemDTO itemDTO,
                                     List<SaleDeliveryOrderSendOutDetailDTO> sendOutItems) {
        // 拣货条目补充数据
        List<SaleDeliveryOrderSendOutDetailDTO> sendOutDetailDTOList = itemDTO.getSendOutItems()
                .stream()
                .peek(dto -> dto.setSaleDeliveryOrderItemId(saleDeliveryOrderItemId))
                .collect(Collectors.toList());
        sendOutItems.addAll(sendOutDetailDTOList);
    }


    /**
     *  根据销售出库单id查询销售出库单明细
     * @param saleDeliveryOrderId 销售出库单id
     * @return 查询结果
     */
    public List<SaleDeliveryOrderItemDTO> queryBySaleDeliveryOrderId(Long saleDeliveryOrderId) {

        LambdaQueryWrapper<SaleDeliveryOrderItemEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SaleDeliveryOrderItemEntity::getSaleDeliveryOrderId, saleDeliveryOrderId);
        List<SaleDeliveryOrderItemDTO> itemDTOList = saleDeliveryOrderItemConvert.convertEntities2DTOList(this.list(wrapper));
        Set<Long> saleDeliveryOrderItemIds = itemDTOList.stream().map(SaleDeliveryOrderItemDTO::getId).collect(Collectors.toSet());

        Map<Long, List<SaleDeliveryOrderPickingItemDTO>> pickItemMap = saleDeliveryOrderPickingItemService.queryAllBySaleDeliveryOrderItemIds(saleDeliveryOrderItemIds)
                .stream()
                .collect(Collectors.groupingBy(SaleDeliveryOrderPickingItemDTO::getSaleDeliveryOrderItemId));

        Map<Long, List<SaleDeliveryOrderSendOutDetailDTO>> sendOutDetailMap = saleDeliveryOrderSendOutDetailService.queryAllBySaleDeliveryOrderItemIds(saleDeliveryOrderItemIds)
                .stream()
                .collect(Collectors.groupingBy(SaleDeliveryOrderSendOutDetailDTO::getSaleDeliveryOrderItemId));

        itemDTOList.forEach(saleDeliveryOrderItem -> {
            saleDeliveryOrderItem.setPickingItems(pickItemMap.getOrDefault(saleDeliveryOrderItem.getId(),Lists.newArrayList()));
            saleDeliveryOrderItem.setSendOutItems(sendOutDetailMap.getOrDefault(saleDeliveryOrderItem.getId(),Lists.newArrayList()));
        });

        return itemDTOList;
    }
}
